package algorithm.math;

import java.util.ArrayList;
import java.util.List;
import java.util.function.IntSupplier;
import java.util.stream.IntStream;

/**
 * 埃氏筛法，找质数.
 * 使用Java Stream 的方式
 * @author Yangzhenyu06
 * @date 2021/7/26 12:09 下午
 */
public class GeneratePrimeNumber {



    private static void findPrimeNumber(){
        // 埃氏筛选法找质数
        IntSupplier primeSupplier = new IntSupplier() {
            private int currPrime = 2;
            private List<Integer> primeList = new ArrayList<>();
            @Override
            public int getAsInt() {
                if(!primeList.isEmpty()){
                    boolean notFind = true;
                    while(notFind){
                        currPrime++;
                        notFind = primeList.stream().anyMatch(n -> currPrime%n==0);
                    }
                }
                primeList.add(currPrime);
                return currPrime;
            }
        };
        IntStream.generate(primeSupplier).limit(10000).forEach(System.out::println);
    }

}
